package com.neu.api.controller;

import com.neu.api.utils.AliYunOSSUtil;
import com.neu.api.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Api(tags = "文档上传")
@RestController
@RequestMapping("file")
public class FileController {

    @Resource
    AliYunOSSUtil aliYunOSSUtil;

    @ApiOperation("上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        //header要和<input type="file" name="header"
        //header对象就包含了<input type="file" name="header"所有信息
//
//        System.out.println(header.getSize());
//        System.out.println(header.getOriginalFilename());
//        System.out.println(header.getName());
//        System.out.println(header.getContentType());
//
//        //保存
//        try {
//            header.transferTo(new File("D://upload//"+header.getOriginalFilename()));
//
//            return Result.ok("成功,name="+name);
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//
//            return Result.ok("失败",500);
//        }

        //file----------->oss
        String originalFilename = file.getOriginalFilename();

        String subfix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String newFileName = UUID.randomUUID().toString().replace("-", "") + subfix;

        String http = aliYunOSSUtil.upload(file.getBytes(),newFileName);

        return Result.ok("访问的路径",http);

    }
}
